<?php
include "Includes/solotrn_sesion.php";
include "Includes/soloFun_Generales.php";

//'Entra cuando va a grabar
//'-----------------------------------------------------------------------------------------------------------------------------------------------

if ($_REQUEST["hidId1"]!="") {
    $tabla = "CANTON";
    switch ($_REQUEST["opc"]) {
        case "M":
            $tipo = "UPDATE";
            $Combo=$_REQUEST["hidId1"];
            $des = strtoupper($_REQUEST["TxtDesc"]);

//		      'Valida si no se ha modificado nada, regresa con OK
            if ($des == $_REQUEST["TxtDesc2"]) {
                header("location: canton_ciudad.php?SoloError=0&CmbModulo=$Combo");
            }

            if(preg_match("/^([A-ZñÑáéíóúÁÉÍÓÚ]*\s?)+$/", utf8_encode($des)) > 0){
                $strSql="$tipo $tabla set ";
                $strSql .="des='$des' ";
                $strSql .="where id_provincia=" . $_REQUEST["hidId1"];
                $strSql .="  and id=" . $_REQUEST["hidId3"];
            }
//echo $CboSupervisor;
//echo $strSql;
            break;
        case "E":
            //consultar si no existen parroquias asignadas a este canton
            $oConexion = new Conexion($MIES["motor_base"], $MIES["db_usuario"], $MIES["db_password"],
                $MIES["basedatos"], $MIES["servidor"], $MIES["flags"], $MIES["encoding"]);
            $sql = "SELECT COUNT(*) FROM PARROQUIA WHERE id_ciudad = " . $_REQUEST["hidId2"];
            
            $conteo = SoloFun_ExecSqlFull($sql, $Arreglo, $oConexion);
            if(is_null($conteo) || $conteo > 0){
                echo "No se puede eliminar porque contiene parroquias";
            }else{
                $tipo = "DELETE";
                $strSql= "$tipo from $tabla";
                $strSql .= " where id_provincia=" . $_REQUEST["hidId1"];
                $strSql .= "  and id=" . $_REQUEST["hidId2"];
            }
            $Combo=$_REQUEST["hidId1"];
            break;
        default:

            $des = strtoupper($_REQUEST["ATxtDesc"]);
            if(preg_match("/^([A-ZñÑáéíóúÁÉÍÓÚ]*\s?)+$/", utf8_encode($des)) > 0){
                $tipo = "INSERT";
                $strSql = "$tipo into $tabla (id_provincia, des) values (";
                $strSql .= $_REQUEST["CmbModulo"] . ", '";
                $strSql .= $des  . "')";
            }
            $Combo=$_REQUEST["CmbModulo"];
            break;
    }

    //$arrQueries = array();
    //array_push($arrQueries, $strSql);
    //array_push($arrQueries, crearQueryLog($tabla, $tipo, $strSql));

    $oConexion = new Conexion($MIES["motor_base"], $MIES["db_usuario"], $MIES["db_password"],
        $MIES["basedatos"], $MIES["servidor"], $MIES["flags"], $MIES["encoding"]);
    SoloFun_ExecSqlFull($strSql, $Arreglo, $oConexion);
//echo $strSql;
    unset($oConexion);
    $oConexion = new Conexion($WORKFLOW["motor_base"], $WORKFLOW["db_usuario"], $WORKFLOW["db_password"],
            $WORKFLOW["basedatos"], $WORKFLOW["servidor"], $WORKFLOW["flags"], $WORKFLOW["encoding"]);
    if(!$oConexion->EjecutarQuery(crearQueryLog($tabla, $tipo, $strSql))) {
    //if(!$oConexion->EjecutarTransaccion($arrQueries)) {
        $strMsg="canton_ciudad.php?SoloError=1&CmbModulo=" . $Combo;
    } else {
        $strMsg="canton_ciudad.php?SoloError=0&CmbModulo="  . $Combo;
    }

    $oConexion->Cerrar();
    header("location: " . $strMsg);
}
//   end if
//'-----------------------------------------------------------------------------------------------------------------------------------------------
//'response.Write(Request("opc"))
//'response.End()
switch ($_REQUEST["opc"]) {
    case "M":
        $boton ="<input type='submit' name='CmdGrabar' value='Modificar' class='SoloStyle_Boton'>";
        $oculto="<input type='hidden' name='opc' value='M'>";
        $titulo="Modificaci&oacute;n de Cant&oacute;n";
        break;
    case "E":
        $boton ="<input type='submit' name='CmdGrabar' value='Eliminar' class='SoloStyle_Boton'>";
        $oculto="<input type='hidden' name='opc' value='E'>";
        $titulo="Eliminaci&oacute;n de Cant&oacute;n";
        break;
}

$strSql = "select des from PROVINCIA where id=" . $_REQUEST["id1"];

$oConexion = new Conexion($MIES["motor_base"], $MIES["db_usuario"], $MIES["db_password"],
        $MIES["basedatos"], $MIES["servidor"], $MIES["flags"], $MIES["encoding"]);
$Desc = SoloFun_ExecSqlFull($strSql, $Arreglo, $oConexion);

?>
<html>
    <head>
        <title>Documentacion</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <link rel="stylesheet" href="SoloStyles/Solostyles.css" type="text/css">
    </head>

    <body topmargin=0 leftmargin=0>
        <!-- Include File="includes/trn_header.asp" -->
        <p align="center" class="SoloStyle_TituloPaginas"><?php echo $titulo;?></p>

        <form name="frmIngreso" action="trn_GrabaCanton.php" method="POST">


            <table border="0" width="70%" align="center">
                <tr>
                    <td class="SoloStyle_Etiqueta" width="20%"><b>Provincia</b></td>
                    <td class="SoloStyle_Texto"width="80%"><?php echo $Desc;?>
                        <input type="hidden" maxlength="50" size="50" name="hidId1" class="SoloStyle_texto" value="<?php echo $_REQUEST["id1"];?>"></td>
                </tr>
                <tr>
                    <td class="SoloStyle_Etiqueta"><b>C&oacute;digo</b></td>
                    <td class="SoloStyle_Texto"><input type="hidden" name="hidId3" value="<?php echo $_REQUEST["id2"];?>">
                        <input type="text" maxlength="50" size="35" name="hidId2" class="SoloStyle_texto" value="<?php echo $_REQUEST["id2"];?>" readonly></td>
                </tr>
                <tr>
                    <td class="SoloStyle_Etiqueta">Cant&oacute;n</td>
                    <td><input name="TxtDesc" type="text" class="SoloStyle_Texto" id="TxtDesc3" size="35" maxlength="30" value="<?php echo $_REQUEST["ds1"];?>">
                        <input name="TxtDesc2" type="hidden" class="SoloStyle_Texto" id="TxtDesc2" size="50" maxlength="30" value="<?php echo $_REQUEST["ds1"];?>"></td>
                </tr>
            </table>

            <br><center><?php echo $boton;
echo $oculto;?></center>
        </form>

    </body>
</html>
